//
//  UIs_RoundedRectangle_Demo1VC.swift
//  APSwift
//
//  Created by jianbo.pei on 2024/8/25.
//

import UIKit

class UIs_RoundedRectangle_Demo1VC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        view.backgroundColor = .lightText
        
        let rrView = RoundedRectView2(frame: view.bounds)
        view.addSubview(rrView)
        rrView.backgroundColor = .white
        
//        let RightHalfCircleV = RightHalfCircleView(frame: view.bounds)
//        view.addSubview(RightHalfCircleV)
        
    }
    

}

extension UIs_RoundedRectangle_Demo1VC {

    class RoundedRectView1: UIView {
        override func draw(_ rect: CGRect) {
            // 获取当前的 CGContext
            guard let context = UIGraphicsGetCurrentContext() else { return }
            
            // 定义矩形的尺寸和圆角半径
            let rect = CGRect(x: 50, y: 200, width: 200, height: 100)
            let cornerRadius: CGFloat = 20.0
            
            // 起点移动到左上角的圆角起始点
            context.move(to: CGPoint(x: rect.minX + cornerRadius, y: rect.minY))
            
            // 绘制上边线和右上角的圆角
            context.addLine(to: CGPoint(x: rect.maxX - cornerRadius, y: rect.minY))
            context.addArc(center: CGPoint(x: rect.maxX - cornerRadius, y: rect.minY + cornerRadius),
                           radius: cornerRadius,
                           startAngle: CGFloat(3 * Double.pi / 2),
                           endAngle: 0,
                           clockwise: false)
            
            // 绘制右边线和右下角的圆角
            context.addLine(to: CGPoint(x: rect.maxX, y: rect.maxY - cornerRadius))
            context.addArc(center: CGPoint(x: rect.maxX - cornerRadius, y: rect.maxY - cornerRadius),
                           radius: cornerRadius,
                           startAngle: 0,
                           endAngle: CGFloat(Double.pi / 2),
                           clockwise: false)
            
            // 绘制下边线和左下角的圆角
            context.addLine(to: CGPoint(x: rect.minX + cornerRadius, y: rect.maxY))
            context.addArc(center: CGPoint(x: rect.minX + cornerRadius, y: rect.maxY - cornerRadius),
                           radius: cornerRadius,
                           startAngle: CGFloat(Double.pi / 2),
                           endAngle: CGFloat(Double.pi),
                           clockwise: false)
            
            // 绘制左边线和左上角的圆角
            context.addLine(to: CGPoint(x: rect.minX, y: rect.minY + cornerRadius))
            context.addArc(center: CGPoint(x: rect.minX + cornerRadius, y: rect.minY + cornerRadius),
                           radius: cornerRadius,
                           startAngle: CGFloat(Double.pi),
                           endAngle: CGFloat(3 * Double.pi / 2),
                           clockwise: false)
            
            // 关闭路径并绘制
            context.closePath()
            context.setFillColor(UIColor.red.cgColor)
            context.fillPath()
            
//            // 如果需要描边，可以如下设置描边颜色和宽度
//            context.addPath(UIBezierPath(roundedRect: rect, cornerRadius: cornerRadius).cgPath)
//            context.setStrokeColor(UIColor.black.cgColor)
//            context.setLineWidth(3.0)
//            context.strokePath()
        }
    }
    
    class RoundedRectView2: UIView {
        override func draw(_ rect: CGRect) {
            guard let context = UIGraphicsGetCurrentContext() else { return }
            
            // 定义矩形的大小
            let rect = CGRect(x: 50, y: 150, width: 200, height: 150)
            let radius: CGFloat = 20.0  // 圆角半径
            
            // 获取矩形的各个点
            let topLeft = CGPoint(x: rect.minX, y: rect.minY)
            let topRight = CGPoint(x: rect.maxX, y: rect.minY)
            let bottomRight = CGPoint(x: rect.maxX, y: rect.maxY)
            let bottomLeft = CGPoint(x: rect.minX, y: rect.maxY)
            
            // 开始路径
            context.beginPath()
            context.move(to: CGPoint(x: topLeft.x + radius, y: topLeft.y)) // 从左上角的右侧一点开始
            
            // 添加圆角并绘制各边
            context.addLine(to: CGPoint(x: topRight.x - radius, y: topRight.y)) // 上边
            context.addArc(tangent1End: topRight, tangent2End: bottomRight, radius: radius) // 右上角圆角
            context.addLine(to: CGPoint(x: bottomRight.x, y: bottomRight.y - radius)) // 右边
            context.addArc(tangent1End: bottomRight, tangent2End: bottomLeft, radius: radius) // 右下角圆角
            context.addLine(to: CGPoint(x: bottomLeft.x + radius, y: bottomLeft.y)) // 下边
            context.addArc(tangent1End: bottomLeft, tangent2End: topLeft, radius: radius) // 左下角圆角
            context.addLine(to: CGPoint(x: topLeft.x, y: topLeft.y + radius)) // 左边
            context.addArc(tangent1End: topLeft, tangent2End: topRight, radius: radius) // 左上角圆角
            
            // 关闭路径并填充颜色
            context.closePath()
            context.setFillColor(UIColor.blue.cgColor)
            context.fillPath()
            
            // 如果需要描边，可以如下设置描边颜色和宽度
            context.setStrokeColor(UIColor.black.cgColor)
            context.setLineWidth(2.0)
            context.strokePath()
        }
    }
}

extension UIs_RoundedRectangle_Demo1VC {

    class RightHalfCircleView: UIView {
        override func draw(_ rect: CGRect) {
            guard let context = UIGraphicsGetCurrentContext() else { return }
            
            // 定义圆的中心点和半径
            let center = CGPoint(x: rect.midX, y: rect.midY)
            let radius: CGFloat = 100.0
            
            // 绘制右半圆，从下方 (90° 或 π/2 弧度) 到上方 (270° 或 3π/2 弧度)，顺时针方向
            context.addArc(center: center,
                           radius: radius,
                           startAngle: CGFloat.pi / 2, // 起始角度: π/2 弧度（下方）
                           endAngle: 3 * CGFloat.pi / 2, // 终止角度: 3π/2 弧度（上方）
                           clockwise: true) // 顺时针方向
            
            // 填充颜色
            context.setFillColor(UIColor.blue.cgColor)
            context.fillPath()
            
            // 如果需要描边，可以如下设置描边颜色和宽度
            context.addArc(center: center,
                           radius: radius,
                           startAngle: CGFloat.pi / 2,
                           endAngle: 3 * CGFloat.pi / 2,
                           clockwise: true)
            context.setStrokeColor(UIColor.black.cgColor)
            context.setLineWidth(2.0)
            context.strokePath()
        }
    }
}
